﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace LingDingSoft.Service
{
    /// <summary>
    /// Token获取
    /// </summary>
    public class AccessTokenService
    {

        public AccessTokenService()
        {

        }
        public Model.TokenModel GetToken(string code)
        {
            try
            {
                Parameter.AccessTokenParameter model = new Parameter.AccessTokenParameter();
                model.client_id = AppConfig.GetAppKey();
                model.code = code;
                model.grant_type = "authorization_code";
                model.redirect_uri = AppConfig.GetCallBackUrl();


                var vParams = new Dictionary<string, string>();
                var vHeader = new Dictionary<string, string>();

                vParams.Add(nameof(model.client_id), HttpUtility.UrlEncode( model.client_id));
                vParams.Add(nameof(model.code), HttpUtility.UrlEncode(model.code));
                vParams.Add(nameof(model.redirect_uri), HttpUtility.UrlEncode(model.redirect_uri));
                vParams.Add(nameof(model.grant_type), HttpUtility.UrlEncode(model.grant_type));

                vHeader.Add("Authorization", _GetAuthorizationValue(AppConfig.GetAppKey() + ":" + AppConfig.GetSecret()));

                var helper = new Http.HttpRequestHelper();
                var vRts = helper.SendRequest(AppConfig.GetTokenUrl(), new Http.ApplicationBodyWriter(vParams), vHeader); 
                return Newtonsoft.Json.JsonConvert.DeserializeObject<Model.TokenModel>(vRts);
            }
            catch (Exception ex)
            {
                //异常处理
                 
                return null;
            }
        
        }
        /// <summary>
        /// Token
        /// </summary>
        /// <returns></returns>
        public Model.TokenModel GetRefreshToken(string refreshToken)
        {
            try
            {
                Parameter.RefreshTokenParameter model = new Parameter.RefreshTokenParameter();
                model.grant_type = "refresh_token";
                model.refresh_token = refreshToken;

                var vParams = new Dictionary<string, string>();
                var vHeader = new Dictionary<string, string>();
                 
                vParams.Add(nameof(model.refresh_token), HttpUtility.UrlEncode(model.refresh_token));
                vParams.Add(nameof(model.grant_type), HttpUtility.UrlEncode(model.grant_type));

                vHeader.Add("Authorization", _GetAuthorizationValue(AppConfig.GetAppKey() + ":" + AppConfig.GetSecret()));

                var helper = new Http.HttpRequestHelper();
                var vRts = helper.SendRequest(AppConfig.GetTokenUrl(), new Http.ApplicationBodyWriter(vParams), vHeader);
                return Newtonsoft.Json.JsonConvert.DeserializeObject<Model.TokenModel>(vRts);
            }
            catch (Exception ex)
            {
                //异常处理

                return null;
            }
             
        }
        private string _GetAuthorizationValue(string str)
        {
            byte[] bytes = Encoding.Default.GetBytes(str);
            string rRts = string.Empty;
            rRts = "Basic " + Convert.ToBase64String(bytes);
            return rRts;
        }

    }
}
